package com.sgb.goods.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @ClassName TimeUtils
 * @Description TODO
 * @Author chongheng.wang
 * @Date 2020/10/29 15:01
 * @Version 1.0
 */
public class TimeUtils {

    /**
     * 获取两个日期之间的间隔天数
     *
     * @return
     */
    public static int getGapDayCount(long startTime, long endTime) {
        Calendar from = Calendar.getInstance();
        from.setTime(new Date(startTime));

        Calendar to = Calendar.getInstance();
        to.setTime(new Date(endTime));

        if (to.getTimeInMillis() < from.getTimeInMillis()) {
            return 0;
        }

        return (int) ((to.getTime().getTime() - from.getTime().getTime()) / (1000 * 60 * 60 * 24));
    }

    /**
     * 获取两个日期之间的间隔年数
     *
     * @return
     */
    public static int getGapYearCount(long startTime, long endTime) {
        Calendar from = Calendar.getInstance();
        from.setTime(new Date(startTime));

        Calendar to = Calendar.getInstance();
        to.setTime(new Date(endTime));
        if (to.getTimeInMillis() < from.getTimeInMillis()) {
            return 0;
        }
        int day = (int) ((to.getTime().getTime() - from.getTime().getTime()) / (1000 * 60 * 60 * 24));
        int year = 0;

        while (day > 365) {
            day = day - 365;
            year++;
        }

        return year;
    }

    /**
     * 按条件返回特定格式文本
     *
     * @return
     */
    public static String getGapCuountTimeFormat(long startTime, long endTime) {
        String timeFormat = "";
        int day = getGapDayCount(startTime, endTime);
        int year = getGapYearCount(startTime, endTime);
        if (day == 0) {//今天
            timeFormat = formatCustomTimeString(startTime, "HH:mm");
        } else if (day == 1) {//昨天
            timeFormat = "昨天";
        } else if (day >= 2 && year == 0) {//一天以上一年以下
//            timeFormat = formatCustomTimeString(startTime, "MM-dd HH:mm");
            timeFormat = formatCustomTimeString(startTime, "MM-dd");
        } else if (year >= 1) {//一年或以上
            timeFormat = String.format("%s年前", year);
//            timeFormat = formatCustomTimeString(startTime, "yyyy-MM-dd");
        }
        return timeFormat;
    }

    public static String formatCustomTimeForString(String time, String formatString) {
        return formatCustomTimeString(null == time || "".equals(time) ? 0 : Long.parseLong(time), formatString);
    }

    /**
     * 时间格式转换
     *
     * @param time         需要转换的时间戳
     * @param formatString 格式举例：yyyy-MM-dd  yyyy-MM-dd  HH:mm:ss  yyyy年MM月dd日
     * @return 返回时间格式
     */
    public static String formatCustomTimeString(long time, String formatString) {
        if(time <= 0) {
            return "/";
        }
        String re_StrTime = null;
        SimpleDateFormat sdf = new SimpleDateFormat(formatString);
        re_StrTime = sdf.format(new Date(time));
        return re_StrTime;

    }

    public static boolean compareTime10s(long startTime, long endTime) {
        return (endTime - startTime) / 1000 >= 10;
    }

    /**
     * 获取当前时间-时间戳
     *
     * @return
     */
    public static long getNowTime() {
        return System.currentTimeMillis();
    }

    /**
     * 前/后?天
     *
     * @param d
     * @param day
     * @return
     */
    public static Date rollDay(Date d, int day) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(d);
        cal.add(Calendar.DAY_OF_MONTH, day);
        return cal.getTime();
    }

    /**
     * 前/后?月
     *
     * @param d
     * @param mon
     * @return
     */
    public static Date rollMon(Date d, int mon) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(d);
        cal.add(Calendar.MONTH, mon);
        return cal.getTime();
    }

    /**
     * 前/后?年
     *
     * @param d
     * @param year
     * @return
     */
    public static Date rollYear(Date d, int year) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(d);
        cal.add(Calendar.YEAR, year);
        return cal.getTime();
    }

    /**
     * 将ms转换成 m′s″或 s″
     *
     * @param ms 毫秒
     * @return m′s″或 s″
     */
    public static String toMinuteAndSecond(long ms) {
        if (ms == 0L) return "0″";
        long minute = ms / 60 * 1000;//分
        long second = (ms - (minute * 60 * 1000)) / 1000; //秒
        if (minute <= 0) {
            return second + "″";
        }
        return minute + "′" + second + "″";
    }

    /**
     * 获取某个日期的时间戳
     *
     * @return
     */
    private static long getMillisNextEarlyMorning() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTimeInMillis();
    }

    /**
     * 获取当年起始时间戳
     *
     * @param timeStamp 毫秒级时间戳
     * @return
     */
    public static long getYearStartTime(long timeStamp) {
        Calendar calendar = Calendar.getInstance();// 获取当前日期
        calendar.setTimeInMillis(timeStamp);
        calendar.add(Calendar.YEAR, 0);
        calendar.add(Calendar.DATE, 0);
        calendar.add(Calendar.MONTH, 0);
        calendar.set(Calendar.DAY_OF_YEAR, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTimeInMillis();
    }

}
